<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="reset-title">重置密码</div>
      <p class="reset-text">
        请设置您的新密码，我们将根据您的新密码重置您的账号密码
      </p>
      <el-form :model="param" :rules="rules" ref="register" size="large">
        <!-- 邮箱号 -->
        <el-form-item prop="user_email">
          <el-input
            type="user_email"
            v-model="param.user_email"
            placeholder="请输入您的新密码"
          >
            <template #prepend>
              <el-icon>
                <Message />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 邮箱验证码 -->
        <el-form-item prop="user_email">
          <el-input
            type="user_email"
            v-model="param.user_captcha"
            placeholder="请输入您的邮箱验证码"
          >
            <template #prepend>
              <el-icon>
                <Position />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 新密码 -->
        <el-form-item prop="user_pwd">
          <el-input
            type="password"
            v-model="param.user_pwd1"
            placeholder="请输入您的新密码"
          >
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 确认新密码 -->
        <el-form-item prop="user_pwd">
          <el-input
            type="password"
            v-model="param.user_pwd2"
            placeholder="请确认您的新密码"
          >
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          size="large"
          @click="submitForm(register)"
          >设置新密码</el-button
        >
        <p class="login-text">
          <el-link type="primary" @click="$router.push('/login')"
            >返回登录</el-link
          >
        </p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
// 导入修改密码的接口
import { findpwdAPI, verifyEmailApi } from "../../api/index";
import { useRouter, useRoute } from "vue-router";

const param = ref({
  user_email: "",
  user_captcha: "",
  user_pwd1: "",
  user_pwd2: "",
});

// 对表单输入信息进行规范
const rules: FormRules = {
  user_email: [
    { required: true, message: "请输入邮箱", trigger: "blur" }, // 留空提示
    {
      // 邮箱验证提示
      pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
      message: "请输入正确的邮箱格式",
      trigger: "blur",
    },
  ],
};

// 操作表单信息的组件
const register = ref<FormInstance>();
const router = useRouter();

// 接收跟随页面跳转过来的参数
const route = useRoute();
if (route.query.email) {
  param.value.user_email = route.query.email as string; // as string  将未知类型的数值转换成字符串类型；
} else {
  router.push("/reset-pwd");
}

// 提交表单信息的方法
const submitForm = (formEl: FormInstance | undefined) => {
  // 参数为false是执行return,没有参数就不让它运行后面的代码
  if (!formEl) return;
  formEl.validate(async (valid: boolean) => {
    if (valid) {
      // 调用api，修改密码逻辑
      let { user_email, user_pwd1, user_pwd2, user_captcha } = param.value;
      let captcha = user_captcha;
      let email = user_email;
      await verifyEmailApi({ email, captcha }) // 注意传参问题！！！一百昏！！！一百昏！！！一百昏！！！一百昏！！！
        .then((content) => {
          console.log(content);
          let { code, msg } = content;
          if (code === 200) {
            // console.log("1111");

            if (user_pwd1 !== user_pwd2) {
              ElMessage.error("两次输入的密码不一致，请重新输入");
            } else {
              // 找回密码接口使用
              let new_user_pwd = user_pwd1;
              findpwdAPI({ user_email, new_user_pwd })
                .then((content) => {
                  let { code, msg } = content.content;
                  if (code === 200) {
                    ElMessage.success(msg);
                    setTimeout(() => {
                      router.push("/login");
                    }, 400);
                  } else {
                    ElMessage.error(msg);
                  }
                })
                .catch(() => {
                  ElMessage.error("服务器异常！");
                });
            }

            // 收工
          } else {
            ElMessage.error(msg);
            console.log("22222");
          }
        })
        .catch(() => {
          ElMessage.error("服务器异常！请联系后台程序员处理！");
        });
    } else {
      return false;
    }
  });
};
</script>

<style scoped>
.login-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
}

.reset-title {
  text-align: center;
  font-size: 22px;
  color: #333;
  font-weight: bold;
  margin-bottom: 10px;
}

.reset-text {
  text-align: center;
  font-size: 14px;
  color: #787878;
  margin-bottom: 40px;
}

.login-container {
  width: 450px;
  border-radius: 5px;
  background: #fff;
  padding: 40px 50px 50px;
  box-sizing: border-box;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-text {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
  color: #333;
}
</style>
